package org.coral.server.common;

import java.util.Collection;

import org.coral.orm.core.base.BasePo;
import org.coral.orm.core.db.process.DataProcessorAsyn;
import org.coral.server.core.config.ConfigManager;
import org.coral.server.core.task.DefaultTokenTaskQueueExecutor;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.core.annotation.Order;
import org.springframework.jdbc.core.JdbcTemplate;

@Configuration
@Order
public class DataComponent {

	private static final Logger log = LoggerFactory.getLogger(DataComponent.class);
	
	@Autowired
	private JdbcTemplate jdbcTemplate;
	
//	/**
//	 * spring上下文注册
//	 * @return
//	 * @throws Exception 
//	 */
//	@Bean
//	public SpringContextHolder springContextHolder() {
//		log.info("BeanComponent loading...SpringContextHolder");
//		return new SpringContextHolder();
//	}
	
	/**
	 * 注册指定的数据处理器
	 * @return
	 */
	@Bean
	public DataProcessorAsyn processor(){
		Collection<Class<BasePo>> cols = RuntimeClassComponent.instance().getClassByType(BasePo.class);
		log.info("BeanComponent loading...DataProcessorAsyn");
		return new DataProcessorAsyn(cols, jdbcTemplate);
	}
	
	/**
	 * 注册指定的扫描器
	 * @return
	 */
	@Bean
	public RuntimeClassComponent runtimeClassManager(){
		log.info("BeanComponent loading...RuntimeClassManager");
		RuntimeClassComponent manager = new RuntimeClassComponent();
		manager.loadClasses(ServerConstant.scanPath);
		return manager;
	}
	
	/**
	 * 注册游戏内的配置管理器
	 * @return
	 * @throws Exception 
	 */
	@Bean
	public ConfigManager configManager() throws Exception {
		log.info("BeanComponent loading...ConfigManager");
		ConfigManager configManager = new ConfigManager();
		configManager.onInitialize();
		return configManager;
	}
	
	/**
	 * 注册默认的公共线程池
	 * @return
	 */
	@Bean
	public DefaultTokenTaskQueueExecutor defaultExecutor() {
		log.info("BeanComponent loading...DefaultTokenTaskQueueExecutor");
		return new DefaultTokenTaskQueueExecutor("default-common",1);
	}

}
